package com.RomaRemez;

import java.util.Arrays;
import java.util.Scanner;

/**
 * Class <code>Palindrome</code> searches
 * palindromes in a string entered from console,
 * ignores spaces and char cases.
 *
 * @version  1.04 05.10.2013
 * @author Roman Remez
 */
public class Palindrome {
    public static void main(String[] args) {
        String inputString = input();

        StringBuilder workString = deleteSpacesAndHighCase(inputString);

        String[] palindromes = getPalindromes(workString);

        showPalindromes(palindromes);

    }

    /**
     * reads string from console and return it
     */
    private static String input(){
        Scanner in = new Scanner(System.in);
        System.out.println("Enter string, please:");
        return in.nextLine();
    }

    /**
     * Prints palindromes
     */
    private static void showPalindromes(String[] palindromes) {
        int length = palindromes.length;
        for (int palindromeIndex = 0; (palindromeIndex < length) && (palindromes[palindromeIndex] != null); palindromeIndex++){
            System.out.println(palindromes[palindromeIndex]);
        }
    }

    /**
     * Method searches for palindromes, put them into string array
     * and returns the array
     */
    private static String[] getPalindromes(StringBuilder workString) {
        String[] palindromesHolder = new String[16];
        int palindromeHolderIndex = 0;
        int length = workString.length();
        StringBuilder resultBuilder = new StringBuilder();
        for (int middle = 1; middle < length - 1; middle++){
            for (int shift = 1; (shift <= middle) && ((middle + shift) < length); shift++){
                if (workString.codePointAt(middle - shift) == workString.codePointAt(middle + shift)){
                    for (int palindromeIndex = middle - shift; palindromeIndex <= middle + shift; palindromeIndex++){
                        resultBuilder.append((char) workString.codePointAt(palindromeIndex));
                    }
                    if (palindromeHolderIndex == palindromesHolder.length){
                        palindromesHolder = grow(palindromesHolder);
                    }
                    palindromesHolder[palindromeHolderIndex++] = resultBuilder.toString();
                    eraseResultBuilder(resultBuilder);
                }else break;
            }
        }
        return palindromesHolder;
    }

    /**
     * Increases the capacity of our String array by 50%
     */
    private static String[] grow(String[] palindromesHolder) {
        int oldCapacity = palindromesHolder.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        palindromesHolder = Arrays.copyOf(palindromesHolder, newCapacity);
        return palindromesHolder;
    }

    /**
     * Erases our StringBuilder which holds last founded palindrome
     */
    private static void eraseResultBuilder(StringBuilder resultBuilder) {
        if (resultBuilder.length() > 0){
            resultBuilder.delete(0, resultBuilder.length());
        }
    }

    /**
     * creates our work string in which we'll look for palindromes
     * takes input string, deletes spaces and converts chars to lower case
     */
    private static StringBuilder deleteSpacesAndHighCase(String inputString) {
        int length = inputString.length();
        int[] arrOfSpaces = new int[length];
        StringBuilder workString = new StringBuilder(inputString.toLowerCase());
        for (int i = 0; i < length; i++){
            arrOfSpaces[i] = workString.indexOf(" ");
            if (arrOfSpaces[i] != -1){
                workString.deleteCharAt(arrOfSpaces[i]);
            }
        }
        return workString;
    }
}
